(function(){
    class Person{
        // public 公共的，可以在类内部和外部访问
        public name:string;
        // protected 受保护的，可以在类内部和子类中访问
        protected age:number;
        // private 私有的，只能在类内部访问
        // 可以在类中添加方法来访问私有属性
        private gender:string;
        constructor(name:string,age:number,gender:string){
            this.name=name;
            this.age=age;
            this.gender=gender;
        }
        // 添加方法来访问私有属性
        getGender(){
            return this.gender;
        }

        // 添加方法来修改私有属性
        setGender(gender:string){
            this.gender=gender;
        }

        // ts中设置getter
        get Gender(){
            console.log('get方法被调用');
            return this.gender;
        }

        // ts中设置setter
        set Gender(gender:string){
            this.gender=gender;
        }
    }

    const per=new Person('孙悟空',18,'男');
    console.log(per.name);
    console.log(per.getGender());
    per.setGender('女');
    console.log(per.getGender());
    console.log(per.Gender);
    per.Gender='女';
    console.log(per.Gender);



    class Student extends Person{
        getGender2(){
            // 报错，因为gender是私有属性，只能在类内部访问
            // return this.gender;
        }
    }


    class Person2{
        // 直接将属性定义在构造函数中，就不用再写this.name=name;这种赋值语句
        constructor(public name:string,public age:number,public gender:string){
        }
    }

    const per2=new Person2('孙悟空',18,'男');
    console.log(per2.name);
    console.log(per2.age);
    console.log(per2.gender);
})()